import Vue from 'vue'
import VueRouter from 'vue-router'

import gk from '../components/gk/gk.vue'
import home from '../components/gk/home.vue'
import wy from '../components/wy/wy.vue'
import jm from '../components/jm/jm.vue'
import welcome from '../components/jm/welcome.vue'
import cxy from '../components/cxy/cxy.vue'
import yrj from '../components/yrj/yrj.vue'
import mhq from '../components/mhq/mhq.vue'



Vue.use(VueRouter)

const routes = [

  {
    path: "/",
    redirect: 'gk'  //重定向
  },
  {
    path:'/gk',
    title:'登录',
    component:gk
  },
  {
    path:'/home',
    title:'首页',
    component:home,
    redirect:"/welcome",
    children:[
      {path:'/welcome',title:'欢迎界面',component:welcome},
      {path:'/wy',title:'水果销售',component:wy},
      {path:'/jm',title:'订单管理',component:jm},
      {path:'/cxy',title:'水果批发',component:cxy},
      {path:'/yrj',title:'库存管理',component:yrj},
      {path:'/mhq',title:'用户信息维护',component:mhq},
    ]
  },
  {
    path:'/wy',
    title:'水果销售',
    component:wy
  },
  {
    path:'/jm',
    title:'订单管理',
    component:jm
  },
  {
    path:'/cxy',
    title:'水果批发',
    component:cxy
  },
  {
    path:'/yrj',
    title:'库存管理',
    component:yrj
  },
  {
    path:'/mhq',
    title:'用户信息维护',
    component:mhq
  },
  {
    path:'/welcome',
    title:'welcome',
    component:welcome
  }
]

const router = new VueRouter({
  routes
})


//导航守卫  通过路由的导航守卫控制访问权限
router.beforeEach((to,from,next) => {
  // 登陆页直接向下执行
  console.log(to.path)
  if(to.path === '/gk') return next();
  // 从sessionStorage获取保存的token值(JSON字符串)
  const tokenStr = window.sessionStorage.getItem("token")
  // 如果没有登录成功,直接通过地址访问,返回登录页面
  if(!tokenStr) return next('/gk')
  // 登录成功则向下
  next();
})

export default router
